import 'package:flutter/material.dart';
import 'package:get/get.dart';

class DialogUtils {
  static void showAlert({
    String title = '提醒',
    String? content,
    String? cancelText = '取消',
    String? confirmText = '确定',
    GestureTapCallback? onConfirm,
    GestureTapCallback? onCancel,
  }) {
    Get.defaultDialog(
      title: title,
      confirm: SizedBox(
        width: 100,
        child: MaterialButton(
          elevation: 0,
          color: Color(0xFF000000),
          textColor: Colors.white,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(16),
          ),
          child: Text(confirmText ?? ''),
          onPressed: () {
            Get.back();
            onConfirm?.call();
          },
        ),
      ),
      titlePadding: const EdgeInsets.only(top: 16, bottom: 6),
      cancel: SizedBox(
        width: 100,
        child: MaterialButton(
          elevation: 0,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(16),
          ),
          color: Color(0xFFbbbbbb),
          textColor: Colors.white,
          child: Text(cancelText ?? ''),
          onPressed: () {
            Get.back();
            onCancel?.call();
          },
        ),
      ),
      radius: 10,
      buttonColor: Color(0xFF000000),
      content: content != null
          ? Padding(
              padding: const EdgeInsets.symmetric(horizontal: 16),
              child: Text(content),
            )
          : null,
      onConfirm: () {
        onConfirm?.call();
        Get.back();
      },
    );
  }

  static void showTextField({
    required TextEditingController controller,
    String? title,
    String? hintText,
    int maxLength = 8,
    bool confirmNeedDismiss = true,
    GestureTapCallback? onConfirm,
  }) {
    var context = Get.context;
    if (context == null) {
      return;
    }
    showDialog(
        context: context,
        builder: (context) => AlertDialog(
              title: Text(title ?? ''),
              shape: RoundedRectangleBorder(
                  borderRadius: BorderRadius.circular(10)),
              content: TextField(
                controller: controller,
                maxLength: maxLength,
                decoration: InputDecoration(
                  hintText: hintText,
                  counterText: '',
                ),
              ),
              actions: <Widget>[
                MaterialButton(
                  color: Color(0xFFbbbbbb),
                  textColor: Colors.white,
                  child: const Text('取消'),
                  onPressed: () {
                    Get.back();
                  },
                ),
                MaterialButton(
                  color: Color(0xFF000000),
                  textColor: Colors.white,
                  child: const Text('确定'),
                  onPressed: () {
                    if (confirmNeedDismiss) {
                      Get.back();
                    }
                    onConfirm?.call();
                  },
                ),
              ],
            ));
  }
}
